package com.tusuo.stds.ui.module;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelList;
import com.tusuo.stds.ui.level.LevelActivity_;
import com.tusuo.stds.util.Utils;

import org.parceler.Parcel;

import java.util.List;

import timber.log.Timber;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
public class LevelListAdapter extends BaseAdapter {

    private List<List<LevelList.Level>> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private boolean mShowStar;

    public LevelListAdapter(Context context, List<List<LevelList.Level>> dataList, boolean showStar) {
        this.context = context;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(context);
        mShowStar = showStar;
    }

    private final class Component {
        LinearLayout layoutLeft;
        TextView tvLeftTitle;
        ImageView ivLeftLock;
        ImageView ivLeftStar1;
        ImageView ivLeftStar2;
        ImageView ivLeftStar3;
        Button btnLeft;

        LinearLayout layoutRight;
        TextView tvRightTitle;
        ImageView ivRightLock;
        ImageView ivRightStar1;
        ImageView ivRightStar2;
        ImageView ivRightStar3;
        Button btnRight;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public List<LevelList.Level> getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Component component = null;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            if (mShowStar) {
                convertView = layoutInflater.inflate(R.layout.level_list_item, null);
            } else {
                // 闯关结果页的关卡列表不需要评星
                convertView = layoutInflater.inflate(R.layout.level_result_level_list_item, null);
            }

            component.layoutLeft = (LinearLayout) convertView.findViewById(R.id.level_list_item_left_layout);
            component.tvLeftTitle = (TextView) convertView.findViewById(R.id.level_list_item_left_tv_title);
            component.ivLeftLock = (ImageView) convertView.findViewById(R.id.level_list_item_left_iv_lock);
            component.btnLeft = (Button) convertView.findViewById(R.id.level_list_item_left_btn);

            component.layoutRight = (LinearLayout) convertView.findViewById(R.id.level_list_item_right_layout);
            component.tvRightTitle = (TextView) convertView.findViewById(R.id.level_list_item_right_tv_title);
            component.ivRightLock = (ImageView) convertView.findViewById(R.id.level_list_item_right_iv_lock);
            component.btnRight = (Button) convertView.findViewById(R.id.level_list_item_right_btn);

            if (mShowStar) {
                component.ivLeftStar1 = (ImageView) convertView.findViewById(R.id.level_list_item_left_iv_star_1);
                component.ivLeftStar2 = (ImageView) convertView.findViewById(R.id.level_list_item_left_iv_star_2);
                component.ivLeftStar3 = (ImageView) convertView.findViewById(R.id.level_list_item_left_iv_star_3);
                component.ivRightStar1 = (ImageView) convertView.findViewById(R.id.level_list_item_right_iv_star_1);
                component.ivRightStar2 = (ImageView) convertView.findViewById(R.id.level_list_item_right_iv_star_2);
                component.ivRightStar3 = (ImageView) convertView.findViewById(R.id.level_list_item_right_iv_star_3);
            }

            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }

        List<LevelList.Level> list = dataList.get(position);

        // 绑定数据

        switch (list.size()) {
            case 0:
                component.layoutLeft.setVisibility(View.INVISIBLE);
                component.layoutRight.setVisibility(View.INVISIBLE);
                return convertView;
            case 1:
                component.layoutLeft.setVisibility(View.VISIBLE);
                component.layoutRight.setVisibility(View.INVISIBLE);
                break;
            case 2:
                component.layoutLeft.setVisibility(View.VISIBLE);
                component.layoutRight.setVisibility(View.VISIBLE);
                break;
            default:
                component.layoutLeft.setVisibility(View.VISIBLE);
                component.layoutRight.setVisibility(View.VISIBLE);
                break;
        }

        LevelList.Level levelLeft = list.get(0);

        component.tvLeftTitle.setText(levelLeft.Title);

        // 0: locked; 10: opening
        if (levelLeft.LockStatus == 0) {
            component.ivLeftLock.setImageResource(R.drawable.level_ic_locked);
            setStar(0, component.ivLeftStar1,
                    component.ivLeftStar2, component.ivLeftStar3);
            component.btnLeft.setBackgroundResource(R.drawable.selector_theme_button_disabled);
            component.btnLeft.setText(R.string.level_list_locked);
        } else {
            component.ivLeftLock.setImageResource(R.drawable.level_ic_opening);
            setStar(levelLeft.CompletionStatus, component.ivLeftStar1,
                    component.ivLeftStar2, component.ivLeftStar3);
            component.btnLeft.setBackgroundResource(R.drawable.selector_theme_button);
            component.btnLeft.setText(R.string.level_list_opening);
        }
        component.btnLeft.setEnabled(true);
        component.btnLeft.setTag(levelLeft.Id);
        component.btnLeft.setOnClickListener(new Listener());
        component.layoutLeft.setTag(levelLeft.Id);
        component.layoutLeft.setOnClickListener(new Listener());

        if (list.size() > 1) {
            LevelList.Level levelRight = list.get(1);

            component.tvRightTitle.setText(levelRight.Title);

            // 0: locked; 10: opening
            if (levelRight.LockStatus == 0) {
                component.ivRightLock.setImageResource(R.drawable.level_ic_locked);
                setStar(0, component.ivRightStar1,
                        component.ivRightStar2, component.ivRightStar3);
                component.btnRight.setBackgroundResource(R.drawable.selector_theme_button_disabled);
                component.btnRight.setText(R.string.level_list_locked);
            } else {
                component.ivRightLock.setImageResource(R.drawable.level_ic_opening);
                setStar(levelRight.CompletionStatus, component.ivRightStar1,
                        component.ivRightStar2, component.ivRightStar3);
                component.btnRight.setBackgroundResource(R.drawable.selector_theme_button);
                component.btnRight.setText(R.string.level_list_opening);
            }
            component.btnRight.setEnabled(true);
            component.btnRight.setTag(levelRight.Id);
            component.btnRight.setOnClickListener(new Listener());
            component.layoutRight.setTag(levelRight.Id);
            component.layoutRight.setOnClickListener(new Listener());
        }

        return convertView;
    }

    private class Listener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (Utils.UI.isFastDoubleClick()) {
                return;
            }

            int levelId = (int) view.getTag();
            Intent intent = new Intent();
            intent.setClass(context, LevelActivity_.class);
            intent.putExtra("levelId", levelId);
            context.startActivity(intent);
        }
    }

    private void setStar(int completionStatus,
                         ImageView ivStar1,
                         ImageView ivStar2,
                         ImageView ivStar3) {
        if (mShowStar) {
            switch (completionStatus) {
                case 0:
                    ivStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 10:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 20:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_selected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 30:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_selected);
                    ivStar3.setImageResource(R.drawable.ic_star_selected);
                    break;
                default:
                    ivStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
            }
        }
    }
}
